package com.joolun.web.controller.scrm;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joolun.system.domain.ScrmDepartment;
import com.joolun.weixin.service.IScrmCustomerAcquisitionService;
import com.joolun.weixin.service.IScrmDepartmentService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.system.domain.ScrmCustomerAcquisition;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.common.core.page.TableDataInfo;

/**
 * 获客助手Controller
 * 
 * @author joolun
 * @date 2025-03-06
 */
@RestController
@RequestMapping("/scrm/acquisition")
public class ScrmCustomerAcquisitionController extends BaseController
{
    @Autowired
    @Lazy
    private IScrmCustomerAcquisitionService scrmCustomerAcquisitionService;

    @Autowired
    @Lazy
    private IScrmDepartmentService iScrmDepartmentService;
    /**
     * 查询获客助手列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ScrmCustomerAcquisition scrmCustomerAcquisition)
    {
        startPage();
        List<ScrmCustomerAcquisition> list = scrmCustomerAcquisitionService.selectScrmCustomerAcquisitionList(scrmCustomerAcquisition);
        // 查询企业信息
        List<ScrmDepartment> departments = iScrmDepartmentService.list(Wrappers.lambdaQuery(ScrmDepartment.class).eq(ScrmDepartment::getType,1));
        Map<String, String> resultListMap = departments.stream().collect(Collectors.toMap(ScrmDepartment::getExtCorpId,ScrmDepartment::getName));
        list.forEach(item->{
            item.setDeptName(resultListMap.get( item.getExtCorpId()));
        });

        return getDataTable(list);
    }

    /**
     * 导出获客助手列表
     */
    @PreAuthorize("@ss.hasPermi('scrm:acquisition:export')")
    @Log(title = "获客助手", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScrmCustomerAcquisition scrmCustomerAcquisition)
    {
        List<ScrmCustomerAcquisition> list = scrmCustomerAcquisitionService.selectScrmCustomerAcquisitionList(scrmCustomerAcquisition);
        ExcelUtil<ScrmCustomerAcquisition> util = new ExcelUtil<ScrmCustomerAcquisition>(ScrmCustomerAcquisition.class);
        util.exportExcel(response, list, "获客助手数据");
    }

    /**
     * 获取获客助手详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scrmCustomerAcquisitionService.selectScrmCustomerAcquisitionById(id));
    }

    /**
     * 新增获客助手
     */
    @Log(title = "获客助手", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScrmCustomerAcquisition scrmCustomerAcquisition)
    {
        scrmCustomerAcquisition.setCreateBy(getUsername());
        return toAjax(scrmCustomerAcquisitionService.insertScrmCustomerAcquisition(scrmCustomerAcquisition));
    }

    /**
     * 修改获客助手
     */
    @PreAuthorize("@ss.hasPermi('scrm:acquisition:edit')")
    @Log(title = "获客助手", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScrmCustomerAcquisition scrmCustomerAcquisition)
    {
        scrmCustomerAcquisition.setCreateBy(getUsername());
        return toAjax(scrmCustomerAcquisitionService.updateScrmCustomerAcquisition(scrmCustomerAcquisition));
    }

    /**
     * 删除获客助手
     */
    @PreAuthorize("@ss.hasPermi('scrm:acquisition:remove')")
    @Log(title = "获客助手", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scrmCustomerAcquisitionService.deleteScrmCustomerAcquisitionByIds(ids));
    }
}
